package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.redis.service.RedisService;
import com.sdy.supermarket.biz.model.GsNotice;
import com.sdy.supermarket.biz.service.GsUserNoticeService;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.model.GsUserNotice;
import com.sdy.common.model.Response;
import com.sdy.supermarket.biz.utils.IpUtil;
import com.sdy.supermarket.biz.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-08-20
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/gsUserNotice")
public class GsUserNoticeController extends BaseController {
    @Autowired
    private GsUserNoticeService gsUserNoticeService;

    @Autowired
    private SsoService ssoService;


//    /*
//     * 发布通知后给用户添加未读消息，如果传入了idList就只给idList列表的用户设置未读，否则视为全部
//     *
//     * @param map (Integer noticeId) (List<Integer>) idList)
//     * @return
//     */
//    @PostMapping("/addAllUserUnReadNotice")
//    public Response addAllUserUnReadNotice(@RequestBody HashMap<String, Object> map) throws BizException {
//        int addAllUserUnReadNotice = gsUserNoticeService.addAllUserUnReadNotice((Integer) map.get("noticeId"), (List<Integer>) map.get("idList"));
//        return addAllUserUnReadNotice == 1 ? Response.success() : Response.error();
//    }

//    /**
//     * 用户读取该通知后，调用此方法将通知修改为已读
//     *
//     * @param map userId 、 noticeId
//     * @return
//     */
//    @PostMapping("/noticeHaveRead")
//    public Response noticeHaveRead(@RequestBody HashMap<String, Object> map) throws BizException {
//        int haveRead = gsUserNoticeService.noticeHaveRead((Integer) map.get("userId"), (Integer) map.get("noticeId"));
//        return haveRead == 1 ? Response.success() : Response.error();
//    }

    /**
     * 通过用户id查询该用户的所有未读通知
     *
     * @return
     */
    @GetMapping("/getUnReadNotice")
    public Response UnReadNotice(HttpServletRequest request, Page<GsNotice> page) throws BizException {
        UserInfo userInfo = ssoService.getUserInfo(request);
        Assert.isNull(userInfo, "未登录");
        List<GsNotice> unReadNotice = gsUserNoticeService.UnReadNotice(userInfo.getUserId());
        Collections.sort(unReadNotice, new Comparator<GsNotice>() {
            @Override
            public int compare(GsNotice o1, GsNotice o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        PageUtils pageUtils = new PageUtils(page.getSize(), page.getCurrent(), unReadNotice);
        return Response.success(pageUtils);
    }

}
